import 'package:flutter/material.dart';

class HiBarrageTransition extends StatefulWidget {
  final Duration speed;
  final Widget child;
  final VoidCallback? onComplete;

  const HiBarrageTransition(
      {super.key, required this.speed, required this.child, this.onComplete});

  @override
  State<StatefulWidget> createState() {
    return HiBarrageTransitionState();
  }
}

class HiBarrageTransitionState extends State<HiBarrageTransition>
    with SingleTickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<Offset> _animation;

  @override
  void initState() {
    super.initState();
    _animationController =
        AnimationController(duration: widget.speed, vsync: this);
    _animationController.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        widget.onComplete?.call();
      }
    });
    Offset begin = const Offset(1.0, 0);
    Offset end = const Offset(-1.0, 0);
    _animation = Tween(begin: begin, end: end).animate(_animationController);
    _animationController.forward();
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SlideTransition(
      position: _animation,
      child: widget.child,
    );
  }
}
